package com.tarena.lbs.content.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface CategoryMapper extends BaseMapper<ArticleCategoryPO> {
    /**
     * 新增分类
     *
     * @param param
     */
    @Insert("insert into article_category(category_name,sort,category_status) values(#{categoryName},#{sort},#{categoryStatus})")
    void addCategory(ArticleCategoryParam param);

    /**
     * 查询分类数量
     *
     * @return Long 分类数量
     */
    @Select("<script>" +
            "select count(*) from article_category " +
            "<where>" +
            "   category_status != 2 " +
            "   <if test='categoryName != null and categoryName != \"\"'>" +
            "       and category_name like concat('%', #{categoryName}, '%')" +
            "   </if>" +
            "   <if test='categoryStatus != null'>" +
            "       and category_status = #{categoryStatus}" +
            "   </if>" +
            "</where>" +
            "</script>")
    Long selectCategoryCount(String categoryName, Integer categoryStatus);

    /**
     * 查询分类列表
     *
     * @return List<ArticleCategoryVO> 分类列表
     */
    @Select("<script>" +
            "select id,category_name as categoryName,sort,category_status as categoryStatus,create_time as createTime,update_time as updateTime from article_category " +
            "<where>" +
            "   category_status != 2 " +
            "   <if test='categoryName != null and categoryName != \"\"'>" +
            "       and category_name like concat('%', #{categoryName}, '%')" +
            "   </if>" +
            "   <if test='categoryStatus != null'>" +
            "       and category_status = #{categoryStatus}" +
            "   </if>" +
            "</where>" +
            "order by sort asc" +
            "</script>")
    List<ArticleCategoryVO> selectCategory(String categoryName, Integer categoryStatus);

    /**
     * 修改分类、删除分类
     *
     * @param articleCategoryParam
     */
    @Update("<script>" +
            "update article_category " +
            "<set>" +
            "   <if test='categoryName != null and categoryName != \"\"'>" +
            "       category_name = #{categoryName}," +
            "   </if>" +
            "   <if test='sort != null'>" +
            "       sort = #{sort}," +
            "   </if>" +
            "   <if test='categoryStatus != null'>" +
            "       category_status = #{categoryStatus}," +
            "   </if>" +
            "</set>" +
            "where id = #{id}" +
            "</script>")
    int updateCategory(ArticleCategoryParam articleCategoryParam);
}
